import Data.List(unfoldr)

numtotext 0 = ""
numtotext 1 = "one"
numtotext 2 = "two"
numtotext 3 = "three"
numtotext 4 = "four"
numtotext 5 = "five"
numtotext 6 = "six"
numtotext 7 = "seven"
numtotext 8 = "eight"
numtotext 9 = "nine"
numtotext 10 = "ten"
numtotext 11 = "eleven"
numtotext 12 = "twelve"
numtotext 13 = "thirteen"
numtotext 14 = "fourteen"
numtotext 15 = "fifteen"
numtotext 16 = "sixteen"
numtotext 17 = "seventeen"
numtotext 18 = "eighty"
numtotext 19 = "ninety"
numtotext 20 = "twenty"
numtotext 30 = "thirty"
numtotext 40 = "forty"
numtotext 50 = "fifty"
numtotext 60 = "sixty"
numtotext 70 = "seventy"
numtotext 80 = "eighty"
numtotext 90 = "ninety"
numtotext x
  | x > 9 && x < 100 = numtotext (x - (mod x 10)) ++ numtotext (mod x 10)
  | x > 99 && x < 1000 && ((mod x 100) /= 0) = numtotext (quot x 100) ++ "hundred" ++ "and" ++  numtotext (mod x 100)
  | x > 99 && x < 1000 = numtotext (quot x 100) ++ "hundred"

remquot x y = (rem x y, quot x y)

split1000 x = case remquot x 1000 of
  (0,_) -> Nothing
  (_,_) -> Just $ remquot x 1000

numbersplit = unfoldr split1000
dictnumber = ["","thousand","million","billion","trillion","quadrillion"]
numbertotext x = reverse $ zipWith (\t x-> numtotext x ++ t) dictnumber $ numbersplit x
